package top.honlnk.common.network;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;
import top.honlnk.common.constant.AdminConstant;
import top.honlnk.pojo.dto.TableToRotaDTO;

import java.io.IOException;
import java.util.List;

@Slf4j
@Component
public class TableToRotaNetwork {
    public List<List<List<String>>> tableToRota(TableToRotaDTO tableToRotaDTO, String url) {

        List<List<List<String>>> dutyTable;

        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            HttpPost httpPost = new HttpPost(url);
            httpPost.setHeader("Content-Type", "application/json; charset=UTF-8");

            // 将TableToRota对象转换为JSON字符串
            String requestBody = convertTableToRotaToJsonUsingJackson(tableToRotaDTO);
            StringEntity entity = new StringEntity(requestBody, "UTF-8");
            httpPost.setEntity(entity);

            try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
                if (response.getStatusLine().getStatusCode() == 200) {
                    HttpEntity responseEntity = response.getEntity();
                    if (responseEntity != null) {
                        String responseString = EntityUtils.toString(responseEntity, "UTF-8");
                        // 将响应结果（JSON格式的二维列表）转换为Java的List<List<List<String>>>
                        dutyTable = parseJsonResponseToList(responseString);
                    } else {
                        log.error(AdminConstant.RESPONSE_BODY_IS_NULL);
                        throw new RuntimeException(AdminConstant.RESPONSE_BODY_IS_NULL);
                    }
                } else {
                    log.error(AdminConstant.REQUEST_FAIL, response.getStatusLine().getStatusCode());
                    throw new RuntimeException(AdminConstant.REQUEST_FAIL);
                }
            }
        } catch (IOException e) {
            log.error(AdminConstant.SEND_REQUEST_FAIL, e);
            throw new RuntimeException(AdminConstant.SEND_REQUEST_FAIL);
        }

        return dutyTable;
    }

    private String convertTableToRotaToJsonUsingJackson(TableToRotaDTO tableToRotaDTO) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        return mapper.writeValueAsString(tableToRotaDTO);
    }

    private List<List<List<String>>> parseJsonResponseToList(String jsonResponse) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        TypeReference<List<List<List<String>>>> typeRef = new TypeReference<>() {};
        return mapper.readValue(jsonResponse, typeRef);
    }
}
